package com.iwhalecloud.bss.kite.cucc.service.customerOrderInformation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.customerOrderInformation.ISerialNumberService;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalInfConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname SerialNumberService
 * @Description 业务号码校验
 * @Author zhang.song
 * @Date 2021-05-06 15:40
 */
@Service
public class SerialNumberService implements ISerialNumberService {

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(SerialNumberService.class);

    @Value("${interface.call.mode:hsf}")
    private String mode;

    /**
     * 业务号码是否可用校验
     * @author zhang.song
     * @date 2021-05-06 15:43
     * @param params
     * @return java.util.Map<java.lang.String,java.lang.String>
     */
    @Override
    public Map<String, String> serialNumberValid(Map<String, String> params) {
        Map<String, String> result = new HashMap<>();
        Map<String, Object> requestMap = new HashMap<>();
        Map<String, Object> uniBssBody = new HashMap<>();
        Map<String, Object> req = new HashMap<>();
        req.put("PROVINCE_CODE", MapUtils.getString(params, "PROVINCE_CODE"));
        req.put("EPARCHY_CODE", MapUtils.getString(params, "EPARCHY_CODE"));
        req.put("AREA_CODE", MapUtils.getString(params, "AREA_CODE"));

        req.put("SERIAL_NUM", MapUtils.getString(params, "SERIAL_NUM"));
        req.put("SERVICE_ID", MapUtils.getString(params, "SERVICE_ID"));
        Map<String, String> tcpCont = new HashMap<>();
        tcpCont.put("APP_KEY", LocalInfConsts.APP_ID);
        req.put("TCP_CONT", tcpCont);
        uniBssBody.put("REQ", req);
        requestMap.put("UNI_BSS_BODY", uniBssBody);
        requestMap.put("OP_CODE", "json_GEuserCenter_UserSeqService_validateSerialNumber");

        String respStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            respStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, requestMap, LocalTokenUtil.getHeaderMap());
        } else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            respStr = infExchangeService.exchange(new JSONObject(requestMap));
        }

        Map<String, Object> respMap = JSON.parseObject(respStr, Map.class);
        Map<String, Object> headMap = MapUtils.getMap(respMap, "UNI_BSS_HEAD");
        if(!"00000".equals(MapUtils.getString(headMap, "RESP_CODE"))) {
            result.put("code", MapUtils.getString(headMap, "RESP_CODE"));
            result.put("message", MapUtils.getString(headMap, "RESP_DESC"));
            return result;
        }

        Map<String, Object> bodyMap = MapUtils.getMap(respMap, "UNI_BSS_BODY");
        Map<String, Object> rspMap = MapUtils.getMap(bodyMap, "RSP");
        // 0000：可用；0001：不可用；9999：系统异常
        String rspCode = MapUtils.getString(rspMap, "RESP_CODE");
        if(KiteStringUtils.isEqual(rspCode, "9999")) {
            result.put("code", rspCode);
            result.put("message", MapUtils.getString(rspMap, "RESP_DESC"));
            return result;
        }

        result.put("code", "00000");
        result.put("rspCode", rspCode);
        result.put("rspDesc", MapUtils.getString(rspMap, "RESP_DESC"));
        return result;
    }

    /**
     * 在途订单/发票查询
     * @author zhang.song
     * @date 2021-05-06 19:50
     * @param checkPassageOrderReq
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @Override
    public Map<String, Object> checkPassageOrder(Map<String, String> checkPassageOrderReq) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> requestMap = new HashMap<>();
        Map<String, Object> uniBssBody = new HashMap<>();
        uniBssBody.put("CHECK_PASSAGE_ORDER_REQ", checkPassageOrderReq);
        requestMap.put("UNI_BSS_BODY", uniBssBody);
        requestMap.put("OP_CODE", "CHECK_PASSAGE_ORDER");

        String respStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            respStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, requestMap, LocalTokenUtil.getHeaderMap());
        } else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            respStr = infExchangeService.exchange(new JSONObject(requestMap));
        }

        Map<String, Object> respMap = JSON.parseObject(respStr, Map.class);
        Map<String, Object> headMap = MapUtils.getMap(respMap, "UNI_BSS_HEAD");
        if(!"00000".equals(MapUtils.getString(headMap, "RESP_CODE"))) {
            result.put("code", MapUtils.getString(headMap, "RESP_CODE"));
            result.put("message", MapUtils.getString(headMap, "RESP_DESC"));
            return result;
        }

        Map<String, Object> bodyMap = MapUtils.getMap(respMap, "UNI_BSS_BODY");
        Map<String, Object> checkPassageOrderRsp = MapUtils.getMap(bodyMap, "CHECK_PASSAGE_ORDER_RSP");
        if(!"0".equals(MapUtils.getString(checkPassageOrderRsp, "STATUS"))) {
            result.put("code", MapUtils.getString(checkPassageOrderRsp, "STATUS"));
            result.put("message", MapUtils.getString(checkPassageOrderRsp, "MESSAGE"));
            return result;
        }

        result.put("code", "00000");
        // false：不在途；true：在途
        result.put("data", MapUtils.getBoolean(checkPassageOrderRsp, "DATA"));
        return result;
    }
}
